package com.yunfei.api.v2.service;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.param.ContractIdParam;
import com.yunfei.api.v2.param.LongTokenParam;
import com.yunfei.api.v2.param.heTongDownLoad.HeTongDownLoadParam;
import com.yunfei.api.v2.param.uploadFileCreateContract.UploadFileCreateContractParam;
import com.yunfei.api.v2.result.UploadFileCreateContract.UploadFileCreateContractJsonRootBean;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.contants.ApplicationConstant;
import com.yunfei.entity.TblobstorageEntity;
import com.yunfei.enums.ResultStatus;
import com.yunfei.utils.DateUtil;
import com.yunfei.utils.http.CopyOfNgnixLiveUtil;
import com.yunfei.utils.http.DownLoadContractHttpUtil;
import com.yunfei.utils.http.HttpIOUtil;
import com.yunfei.utils.http.UploadFileCreateContractHttpUtil;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.elecContBeneAcctDtl.bo.ElecContBeneAcctDtlBo;
import com.yunfei.yunhetong.elecContBeneAcctDtl.service.ElecContBeneAcctDtlService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
* @Description: 上传文件生成合同
* @Author: zhengyunfei
* @Date: 2019/8/27
* @time:10:52
*/
@Service
public class UploadFileCreateContractService {
    @Autowired
    private GetLongTokenService getLongTokenService;
    @Autowired
    private ElecContBeneAcctDtlService elecContBeneAcctDtlService;
    @Autowired
    private Environment env;
    @Autowired
    private DownLoadContractService downLoadContractService;
    /**
    * @Description: 上传文件生成合同
    * @Author: zhengyunfei
    * @Date: 2019/9/6
    * @time:14:24
    */
    public ResultModel uploadFileCreateContract(UploadFileCreateContractParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String token=getLongTokenService.getLongToken(new LongTokenParam());
            //获取合同存放服务器地址
            String savePath=env.getProperty(ApplicationConstant.FILE_IMAGE_PATH);
            String domain=env.getProperty(ApplicationConstant.IMAGE_SERVER_DOMAIN);
           String contractTitle=param.getContractTitle();
            HttpIOUtil httpIOUtil=new HttpIOUtil();
            String resultJson= httpIOUtil.uploadFileCreateContract(token,"D:\\fundcrm\\template\\rengoushu\\认购协议书模板-浙江贵直股权投资基金管理有限公司.docx",contractTitle);
            System.out.println("调用接口返回结果======"+resultJson);
            JSONObject jsonObject=JSONObject.fromObject(resultJson);
            UploadFileCreateContractJsonRootBean uploadFileCreateContractJsonRootBean= (UploadFileCreateContractJsonRootBean) JSONObject.toBean(jsonObject,UploadFileCreateContractJsonRootBean.class);
            long contractId=uploadFileCreateContractJsonRootBean.getData().getContractId();
            //根据合同id下载合同
            HeTongDownLoadParam heTongDownLoadParam=new HeTongDownLoadParam();
            heTongDownLoadParam.setIdType("0");
            heTongDownLoadParam.setIdContent(contractId+"");
            resultModel=downLoadContractService.downloadContract(heTongDownLoadParam);
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }


}
